package com.tpp.scheduler.controller;

import com.tpp.comm.vo.Result;
import com.tpp.scheduler.service.SchedulerSeatPriceService;
import com.tpp.scheduler.vo.*;
import com.tpp.scheduler.service.ConfirmOrderService;
import com.tpp.scheduler.service.SchedulerSeatService;
import com.tpp.scheduler.service.SchedulerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
public class SchedulerController {

    @Resource
    private SchedulerService schedulerService;

    @Resource
    private SchedulerSeatPriceService schedulerSeatPriceService;

    @Resource
    private SchedulerSeatService schedulerSeatService;

    @Resource
    private ConfirmOrderService confirmOrderService;

    @GetMapping("/p/queryItemScheduler")
    public Result queryItemScheduler(Integer itemId){
        List<SchedulerVo> schedulerVos = schedulerService.queryItemScheduler(itemId);
        return Result.success(schedulerVos);
    }

    @GetMapping("/p/queryItemPrice")
    public Result queryItemPrice(Integer scheduleId){
        List<SchedulerSeatPriceVo> schedulerSeatPriceVos = schedulerSeatPriceService.queryItemPrice(scheduleId);
        return Result.success(schedulerSeatPriceVos);
    }

    @GetMapping("/p/queryOriginCinemaSeatSeatArray")
    public Result queryOriginCinemaSeatSeatArray(Integer scheduleId){
        SeatVo seatVo = schedulerSeatService.queryOriginCinemaSeatSeatArray(scheduleId);
        return Result.success(seatVo);
    }

    @PostMapping("/p/queryCinemaSeatArrayBySchedulerId")
    public Result queryCinemaSeatArrayBySchedulerId(@RequestBody SeatDetailVo seatDetailVo){
        SeatDetailVo vo = schedulerSeatService.queryCinemaSeatArrayBySchedulerId(seatDetailVo.getScheduleId(),seatDetailVo.getCinemaId());
        return Result.success(vo);
    }

    @PostMapping("/v/confirmOrder")
    public Result confirmOrder(@RequestBody OrderVo orderVo){
        try {
            String orderNo = confirmOrderService.confirmOrder(orderVo);
            Map<String,Object> map = new HashMap<>();
            map.put("orderNo",orderNo);
            return Result.success(map);
        }catch(RuntimeException e){
            System.out.println(e.getMessage());
            return Result.fail(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.fail("服务拥挤，请一会再试试吧");
        }
    }

}
